﻿using BaseUtil;

namespace Assets.Editor.XmlBufFile
{
    public class XMLBufFileWritter
    {
        public static bool TransformObj(object obj, string filepath, string fullpath, string bytesFilePath = "")
        {
            if (obj != null)
            {
            }
            else
            {
#if USE_LOGWRAPPER
                LogWrapper.LogError("ParserXML fail  ", filepath);
#endif
                return false;
            }

            NetBuffer buff = new NetBuffer(10 * 1024);
            NetOutStream outs = new NetOutStream(buff);

            outs.Write(obj.GetType().Name);
            if (obj is IXMLData)
            {
                IXMLData ixmld = obj as IXMLData;
                ixmld.WriteToBuf(outs);
            }
            else
            {
#if USE_LOGWRAPPER
                LogWrapper.LogError("Object2Buff error ", obj.GetType().Name, " is not ixmldata");
#endif
                return false;
            }

            string file_path;

            if (bytesFilePath == "")
            {
                file_path = fullpath + ".bytes";
            }
            else
            {
                file_path = bytesFilePath;
            }


            XMLBuffHelper.SaveBuffToFile(buff.buffer, buff.length, file_path);

            return true;
        }
    }
}